package uc;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import cn.onekit.android.ACTIVITY;
import co.chuangdian.oflauncher.R;

public class IconView extends FrameLayout {

	public static int MINI_HEIGHT = 70;
	public static int HEIGHT = 110;

	public IconView(Context context) {
		super(context);
		init();
	}

	public IconView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	ImageView iconImageView;
	TextView letterTextView;
	TextView titleTextView;
	ImageView selectedImageView;

	void init() {

		View.inflate(getContext(), R.layout.uc_iconview, this);
		iconImageView = (ImageView) this.findViewById(R.id.iconImageView);
		letterTextView = (TextView) this.findViewById(R.id.letterTextView);
		titleTextView = (TextView) this.findViewById(R.id.titleTextView);
		selectedImageView = (ImageView) this.findViewById(R.id.selectedImageView);
	}

	public void setData(Drawable icon, String title) {
		boolean isLetter = (icon == null);
		iconImageView.setVisibility(isLetter ? View.GONE : View.VISIBLE);
		letterTextView.setVisibility(isLetter ? View.VISIBLE : View.GONE);
		if (isLetter) {
			letterTextView.setText(title.substring(0, 1));
		} else {
			iconImageView.setImageDrawable(icon);
		}
		titleTextView.setText(title);
	}

	public void hideTitle(boolean hide) {
		titleTextView.setVisibility(hide ? View.GONE : View.VISIBLE);
		AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,
				hide ? ACTIVITY.dp2px(MINI_HEIGHT) : ACTIVITY.dp2px(HEIGHT));
		this.setLayoutParams(layoutParams);
	}

	boolean select;

	public boolean getSelect() {
		return select;
	}

	public void setSelect(boolean select) {
		this.select = select;
		selectedImageView.setVisibility(select ? View.VISIBLE : View.INVISIBLE);
	}
}
